package com.pds.web.opsOrganization.controller;

import cn.hutool.core.util.ObjectUtil;
import com.pds.common.constant.Constants;
import com.pds.common.core.controller.BaseController;
import com.pds.common.core.domain.R;
import com.pds.common.core.page.TableDataInfo;
import com.pds.common.utils.poi.ExcelUtil;
import com.pds.utils.ExcelTemplateUtil;
import com.pds.web.device.entity.PdsTestOrganizationDevice;
import com.pds.web.opsOrganization.domain.OpsOrganizationInfo;
import com.pds.web.opsOrganization.service.IOpsOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 运维机构管理
 */
@RestController
@RequestMapping("/opsOrganization")
public class OpsOrganizationController extends BaseController {

    @Autowired
    private IOpsOrganizationService opsOrganizationService;
    /**
     * 环保局新增运维机构
     * @return
     */
    @PostMapping("add")
    @PreAuthorize("@ss.hasPermi('admin:ops:add')")
    public R addOrganization(@RequestBody OpsOrganizationInfo info) throws Exception {
        if(ObjectUtil.isEmpty(info.getOpsId())) {
            //无id，新增
            opsOrganizationService.addOrganization(info);
        }else{
            //有id修改
            opsOrganizationService.updateOrganization(info,1);
        }
        return R.ok();
    }


    /**
     * 运维机构信息自己修改信息
     * @param info
     * @return
     * @throws Exception
     */
    @PostMapping("updateSelf")
    @PreAuthorize("@ss.hasPermi('ops:self:update')")
    public R updateOrganizationSelf(@RequestBody OpsOrganizationInfo info) throws Exception {
        opsOrganizationService.updateOrganization(info,2);
        return R.ok();
    }

    /**
     * 查询运维机构列表
     * @return
     */
    @GetMapping("list")
    public TableDataInfo getOrganizationList(OpsOrganizationInfo info){
        startPage();
        List<OpsOrganizationInfo> list = opsOrganizationService.getOrganizationList(info);
        return getDataTable(list);
    }


    /**
     * 查询运维机构信息
     * @return
     */
    @GetMapping("getInfoById")
    public R getInfoById(@RequestParam(name = "opsId",required = false)String opsId) {
        OpsOrganizationInfo info = opsOrganizationService.getInfoById(opsId);
        return R.ok(info);
    }

    /**
     * 修改运维机构状态：
     * @param id
     * @param status 0正常、1禁用、2删除
     * @return
     */
    @PreAuthorize("@ss.hasPermi('admin:ops:add')")
    @PostMapping("updateStatus")
    public R updateStatus(@RequestBody OpsOrganizationInfo info) throws Exception {
        opsOrganizationService.updateStatus(info.getOpsId(),info.getStatus());
        return R.ok();
    }

    /**
     * 运维机构批量导入模板下载
     * @param response
     */
    @GetMapping("downloadTemplate")
    public void downloadTemplate(HttpServletResponse response){
        ExcelTemplateUtil.downloadTemplate(response,"opsInfoTemplate.xlsx","运维机构导入模板");
    }

    /**
     * 环保局批量导入运维机构
     */
    @PostMapping("batchImport")
    @PreAuthorize("@ss.hasPermi('admin:ops:add')")
    public R batchImport(MultipartFile file) throws Exception {
        //获取文件名+后缀
        String filename = file.getOriginalFilename();
        if (filename != null) {
            //获取其后缀
            String extension = filename.substring(filename.lastIndexOf(".") + 1);
            if (!(extension.equals("xls") || extension.equals("xlsx"))) {
                //此处为自定义异常捕获,可使用其他方式
                throw new RuntimeException("文件格式有误,请检查上传文件格式");
            }
        }
        //获取文件的输入流
        ExcelUtil excelUtil = new ExcelUtil(OpsOrganizationInfo.class);
        //校验表头，防止模板被改
        String topRow = excelUtil.getTopRow(file.getInputStream());
        if(!Constants.OPS_INFO_HEADERS.equals(topRow)){
            throw new Exception(file.getOriginalFilename()+"文件表头被更改，请用正确的模板上传!");
        }
        //读取数据
        List<OpsOrganizationInfo> list = excelUtil.importExcel(file.getInputStream());
        if(list==null && list.size()<1){
            throw new Exception(file.getOriginalFilename()+"没有读到需要导入的数据，请检查！");
        }
        opsOrganizationService.batchInsert(list);
        return R.ok();
    }


}
